package gamesys;

import java.awt.*;
import java.awt.event.*;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.*;

@SuppressWarnings("serial")
public class NetworkOptionsPanel extends GamePanel implements ActionListener {

	private JPanel centerOptions;
	private GridLayout layout;
	
	private JTextField name;
	private JTextField ipAddress;
	private JTextField port;
	private JCheckBox hostCheck;
	
	public NetworkOptionsPanel()
	{
		this.setUseCanvas(false);
	
		layout = new GridLayout(10,1);
		layout.setVgap(10);
		centerOptions = new JPanel();
		centerOptions.setLayout(new GridLayout(1,2,0,0));
	}
	
	protected void intitialize()
	{
		this.setVisible(false);
		this.add(centerOptions,BorderLayout.CENTER);
	    hostCheck = new JCheckBox("Host Game?");
		JButton start = new JButton("Start");
		start.addActionListener(this);
		
		createLabels();
		createFields();

		this.add(start,BorderLayout.SOUTH);
		
		
		this.setVisible(true);
	}
	
	private void createLabels()
	{
		JPanel panel = new JPanel();
		panel.setLayout(layout);
		panel.add(new JLabel("Name:"));
		panel.add(new JLabel("IP:"));
		panel.add(new JLabel("Port:"));
		panel.add(new JLabel("Your IP:"));
	
		centerOptions.add(panel);
	}
	
	private void createFields()
	{
		InetAddress address;
		String ip = "IP Could Not be found (dont ask why)";
		try {
			address = InetAddress.getLocalHost();
			ip = address.getHostAddress();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		JPanel panel = new JPanel();
		panel.setLayout(layout);
		name = new JTextField(300);
		ipAddress = new JTextField(ip);
		port = new JTextField("1024");
		hostCheck = new JCheckBox("Host Game?");
		panel.add(name);
		panel.add(ipAddress);
		panel.add(port);
		//panel.add(new JLabel(address.getHostAddress() + ""));
		panel.add(hostCheck);
		panel.setSize(300, 300);
		centerOptions.add(panel);
	
	}
	@Override
	protected void render(Graphics2D g) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		NetworkGame game = new NetworkGame(name.getText(),hostCheck.isSelected());
		game.setAddress(ipAddress.getText());
		game.setPort(Integer.parseInt(port.getText()));
		setPanel(game);
	}

}
